﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <style>
        .layui-form-label {
            width: 100px;
        }

        .layui-form-select dl {
            max-height: 150px;
        }

        .layui-input-block {
            margin-left: 130px;
        }
    </style>
</head>

<body>
    <div class="layui-form" id="popup-from" style="padding: 20px 30px 0 0;">
        <input type="text" name="cId" value="" style="display: none;" />
        <div class="layui-form-item layui-row layui-col-space20">
            <div class="layui-col-xs6">
                <label class="layui-form-label">用户</label>
                <div class="layui-input-block">
                    <input type="text" name="cUserId" lay-verify="required" placeholder="请输入用户"
                           autocomplete="off" class="layui-input" style="display:none">
                    <input type="text" id="cUserName" lay-verify="required" 
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">Ip</label>
                <div class="layui-input-block">
                    <input type="text" name="cIp" placeholder="请输入Ip"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">封禁时间</label>
                <div class="layui-input-block">
                    <select name="cTerm" lay-verify="required" lay-filter="cTerm-select-filter">
                        <option value="">请选择时间</option>
                        <option value="一天">一天</option>
                        <option value="一周">一周</option>
                        <option value="一月">一月</option>
                        <option value="一季">一季</option>
                        <option value="半年">半年</option>
                        <option value="一年">一年</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs12">
                <label class="layui-form-label">封禁时间段</label>
                <div class="layui-input-block">
                    <div class="layui-input-inline">
                        <input type="text" autocomplete="off" name="cForbiddenStartTime" class="layui-input" placeholder="开始日期" disabled>
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline">
                        <input type="text" autocomplete="off" name="cForbiddenEndTime" class="layui-input" placeholder="结束日期" disabled>
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12">
                <label class="layui-form-label">封禁原因</label>
                <div class="layui-input-block">
                    <textarea name="cDescription" placeholder="请输入封禁原因"
                              class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-layout-admin layui-hide">
                <div class="layui-input-block">
                    <div class="layui-footer" style="left: 0; text-align: right">
                        <button class="layui-btn" lay-filter="form-submit" id="form-submit" type="button" lay-submit>
                            立即提交
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form'], function () {
            var $ = layui.$,
                form = layui.form,
                admin = layui.admin
            var id = $.getParam("id");
            var cUserId = $.getParam("cUserId");
            var cUserName = $.getParam("cUserName");
            $("#cUserName").val(cUserName);

            form.on('select(cTerm-select-filter)', function (data) {
                let currentDate = new Date(); // 获取当前日期
                let cForbiddenStartTime = $("input[name='cForbiddenStartTime']").val();
                if (cForbiddenStartTime) {
                    currentDate = new Date(cForbiddenStartTime);
                }

                const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
                if (data.value == "一天") {
                    currentDate.setDate(currentDate.getDate() + 1); // 增加1天
                }
                else if (data.value == "一周") {
                    currentDate.setDate(currentDate.getDate() + 7);
                }
                else if (data.value == "一月") {
                    currentDate.setMonth(currentDate.getMonth() + 1);
                }
                else if (data.value == "一季") {
                    currentDate.setMonth(currentDate.getMonth() + 3);
                }
                else if (data.value == "半年") {
                    currentDate.setMonth(currentDate.getMonth() + 6);
                }
                else if (data.value == "一年") {
                    currentDate.setFullYear(currentDate.getFullYear() + 1); // 增加1一年
                }
                $("input[name=cForbiddenEndTime]").val(currentDate.toLocaleDateString('zh-CN', options));
            });

            //获取表单值
            if (id != "") {
                admin.req({
                    type: 'get',
                    url: "/api/Blacklist/GetBlacklistForm?id=" + id,
                    success: function (r) {
                        if (r.code == 200) {
                            r.data.cForbiddenStartTime = r.data.cForbiddenStartTime.substring(0, 10);
                            r.data.cForbiddenEndTime = r.data.cForbiddenEndTime.substring(0, 10);
                            $("#popup-from").SetFormData(r.data);
                            form.render();
                        }
                    }
                });
            } else {
                $("input[name=cUserId]").val(cUserId);
                $("input[name=cForbiddenStartTime]").val(new Date().toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }));
            }
            //保存表单
            form.on('submit(form-submit)', function (data) {
                var field = data.field; //获取提交的字段

                //提交
                admin.req({
                    type: 'post',
                    url: "/api/Blacklist/EditBlacklist",
                    data: field,
                    success: function (data) {
                        if (data.code == 200) {
                            parent.layer.msg('操作成功!', { icon: 1, time: 1000 }, function () {
                                parent.layui.table.reload(parent.areaName);
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg('操作失败!\n' + data.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
            });
        })
    </script>
</body>

</html>